package work.com;

import java.util.Scanner;
import java.util.Stack;

public class Exam05 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入计算公式：");
        String str = scanner.next();
        System.out.println(calculator(str));

//        double result = evaluateExpression(expression);
//        System.out.println("计算结果：" + result);
    }

    //    public static double evaluateExpression(String expression) {
//        Stack<Double> operands = new Stack<>();
//        Stack<Character> operators = new Stack<>();
//
//        for (int i = 0; i < expression.length(); i++) {
//            char ch = expression.charAt(i);
//
//            if (ch == ' ') {
//                continue;
//            } else if (ch >= '0' && ch <= '9') {
//                StringBuilder numBuilder = new StringBuilder();
//                while (i < expression.length() && ((expression.charAt(i) >= '0' && expression.charAt(i) <= '9') || expression.charAt(i) == '.')) {
//                    numBuilder.append(expression.charAt(i));
//                    i++;
//                }
//                i--;
//                double num = Double.parseDouble(numBuilder.toString());
//                operands.push(num);
//            } else if (ch == '+' || ch == '-' || ch == '*' || ch == '/') {
//                while (!operators.isEmpty() && hasHigherPriority(operators.peek(), ch)) {
//                    double operand2 = operands.pop();
//                    double operand1 = operands.pop();
//                    char operator = operators.pop();
//                    operands.push(applyOperator(operand1, operand2, operator));
//                }
//                operators.push(ch);
//            }
//        }
//
//        while (!operators.isEmpty()) {
//            double operand2 = operands.pop();
//            double operand1 = operands.pop();
//            char operator = operators.pop();
//            operands.push(applyOperator(operand1, operand2, operator));
//        }
//
//        return operands.pop();
//    }
//
//    public static boolean hasHigherPriority(char op1, char op2) {
//        return (op1 == '*' || op1 == '/') && (op2 == '+' || op2 == '-');
//    }
//
//    public static double applyOperator(double operand1, double operand2, char operator) {
//        switch (operator) {
//            case '+':
//                return operand1 + operand2;
//            case '-':
//                return operand1 - operand2;
//            case '*':
//                return operand1 * operand2;
//            case '/':
//                return operand1 / operand2;
//            default:
//                throw new IllegalArgumentException("无效的运算符：" + operator);
//        }
//    }
    public static int calculator(String str) {
        str = str.replace(" ", "");
        while (str.contains("*") || (str.contains("/"))) {//乘除优先级
            int index = str.indexOf("*");
            int index2 = str.indexOf("/");
            if (index == -1) {//没有乘法
                index = index2;
            } else if (index2 != -1 && index2 < index) {//有除法，除法再乘法前
                index = index2;
            }
            //乘除运算左右两边的数字下标
            int left = index - 1;
            int right = index + 1;
            while (left >= 0 && Character.isDigit(str.charAt(left))) {
                left--;
            }
            while (right < str.length() && Character.isDigit(str.charAt(right))) {
                right++;
            }
            //找到左右两边的数字
            int leftnumber = Integer.parseInt(str.substring(left + 1, index));
            int rightnumber = Integer.parseInt(str.substring(index + 1, right));
            int c = 0;
            if (str.charAt(index) == '*') {
                c = leftnumber * rightnumber;
            } else {
                c = leftnumber / rightnumber;
            }
            str = str.substring(0, left + 1) + c + str.substring(right);
        }


        while (str.contains("+") || str.contains("-")) {
            int index = str.indexOf("+");
            int index2 = str.indexOf("-",1);
            if (index == -1) {//没有乘法
                index = index2;
            } else if (index2 != -1 && index2 < index) {//有除法，除法再乘法前
                index = index2;
            }
            if (index == -1){
                break;
            }
            //乘除运算左右两边的数字下标
            int left = index - 1;
            int right = index + 1;
            while (left>= 0 && ( Character.isDigit(str.charAt(left)) || str.charAt(left) == '-')) {
                left--;
            }
            while (right < str.length() && Character.isDigit(str.charAt(right))) {
                right++;
            }
            //找到左右两边的数字
            int leftnumber = Integer.parseInt(str.substring(left + 1, index));
            int rightnumber = Integer.parseInt(str.substring(index + 1, right));

            int c = 0;
            if (str.charAt(index) == '+') {
                c = leftnumber + rightnumber;
            } else {
                c = leftnumber - rightnumber;
            }
            str = str.substring(0, left + 1) + c + str.substring(right);
        }
        return Integer.parseInt(str);
    }
//        while (str.indexOf('+') != -1 || str.indexOf('-') != -1) {
//            int index = str.indexOf('+');
//            int index2 = str.indexOf('-', 1); // 避免找到符号
//            if (index == -1) {
//                index = index2;
//            } else if (index2 != -1 && index2 < index) {
//                index = index2;
//            }
//            if (index == -1) {
//                break;
//            }
//            int left = index - 1;
//            int right = index + 1;
//            // 找左边的上一个运算符
//            while (left >= 0 && (Character.isDigit(str.charAt(left)) || str.charAt(left) == '-')) {
//                left--;
//            }
//            // 找右边的下一个运算符
//            while (right < str.length() && Character.isDigit(str.charAt(right))) {
//                right++;
//            }
//
//            int leftNumber = Integer.parseInt(str.substring(left + 1, index));
//            int rightNumber = Integer.parseInt(str.substring(index + 1, right));
//            int c = 0;
//
//            if (str.charAt(index) == '+') {
//                c = leftNumber + rightNumber;
//            } else {
//                c = leftNumber - rightNumber;
//            }
//            str = str.substring(0, left + 1) + c + str.substring(right);
//        }
//        System.out.println(str);
//        return Integer.parseInt(str);
}
